//
//  MSBookersOpeningRankController.m
//  觅尚
//
//  Created by No.8 on 2019/3/27.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSBookersOpeningRankController.h"
#import "MSBookersOpenRankCell.h"
#import "MSBookerRankModel.h"
#define pageSize 20

@interface MSBookersOpeningRankController ()<UITableViewDataSource,UITableViewDelegate>
{
    CustomNavigationBar *naviagtionBar;
    
    NSInteger page;
    
    NSInteger count;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *RankArray;

@end

@implementation MSBookersOpeningRankController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.RankArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}

- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    [self tableView];
    
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}
- (void)FootderRefreshReloadData{
    
    [self.RankArray removeAllObjects];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
//    WYCLog(@"---------%@",params);
    
    [HTTPRequest POST:MSgetOpenRoomRanking params:params succeed:^(id responseObject) {
//        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_footer endRefreshing];
            self.RankArray = [MSBookerRankModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRecordList"]];
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page--;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)ReloadData{
    [self.RankArray removeAllObjects];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
        WYCLog(@"---------%@",params);
    
    [HTTPRequest POST:MSgetOpenRoomRanking params:params succeed:^(id responseObject) {
        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            count = [responseObject[@"data"][@"count"] integerValue];
            NSMutableArray *temporaryArray = [MSBookerRankModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRecordList"]];
            [temporaryArray addObjectsFromArray:self.RankArray];
            self.RankArray = temporaryArray;
            
//            self.RankArray = [MSBookerRankModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        }else{
             [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.RankArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSBookersOpenRankCell * cell = [MSBookersOpenRankCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.BookerRankModel = self.RankArray[indexPath.row];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.RankingLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
    
    WYCLog(@"----------%@",cell.RankingLabel.text);
    if ([cell.RankingLabel.text isEqualToString:@"1"] || [cell.RankingLabel.text isEqualToString:@"2"] || [cell.RankingLabel.text isEqualToString:@"3"]) {
        cell.RankingLabel.textColor = [UIColor orangeColor];
    }else{

        cell.RankingLabel.textColor = [UIColor whiteColor];
    }
    return cell;
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"订房人开房排名";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
